// GameOptionsPanel.xaml.cs
//
// Copyright (c) 2007 Mike Strobel
//
// This source code is subject to the terms of the Microsoft Reciprocal License (Ms-RL).
// For details, see <http://www.opensource.org/licenses/ms-rl.html>.
//
// All other rights reserved.

using System;
using System.Windows.Media.Imaging;

using Microsoft.Practices.ServiceLocation;

using Supremacy.Game;
using Supremacy.Types;

using System.Linq;

using Supremacy.Utility;

namespace Supremacy.Client
{
    /// <summary>
    /// Interaction logic for GameOptionsPanel.xaml
    /// </summary>
    public partial class GameOptionsPanel
    {
        #region Fields
        private GameOptions _options;
        #endregion

        #region Events
        public event DefaultEventHandler OptionsChanged;
        #endregion

        #region Constructors
        public GameOptionsPanel()
        {
            InitializeComponent();

            lstGalaxySize.ItemsSource = EnumHelper.GetValues<GalaxySize>();
            lstGalaxyShape.ItemsSource = EnumHelper.GetValues<GalaxyShape>();
            lstStarDensity.ItemsSource = EnumHelper.GetValues<StarDensity>();
            lstPlanetDensity.ItemsSource = EnumHelper.GetValues<PlanetDensity>();
            lstTechLevel.ItemsSource = EnumHelper.GetValues<StartingTechLevel>();
            lstMinorRaces.ItemsSource = EnumHelper.GetValues<MinorRaceFrequency>();

            lstGalaxySize.SelectionChanged += (sender,args) => OnOptionsChanged();
            lstGalaxyShape.SelectionChanged += (sender, args) => { OnOptionsChanged(); UpdateGalaxyImage(); };
            lstStarDensity.SelectionChanged += (sender, args) => OnOptionsChanged();
            lstPlanetDensity.SelectionChanged += (sender, args) => OnOptionsChanged();
            lstTechLevel.SelectionChanged += (sender, args) => OnOptionsChanged();
            lstMinorRaces.SelectionChanged += (sender, args) => OnOptionsChanged();

            this.Loaded += (sender, args) => UpdateGalaxyImage();

            this.Options = ServiceLocator.Current.GetInstance<GameOptions>();
        }
        #endregion

        #region Properties
        public GameOptions Options
        {
            get
            {
                if (_options == null)
                    _options = ServiceLocator.Current.GetInstance<GameOptions>();
                return _options;
            }
            set
            {
                _options = value;
                this.DataContext = _options;
                if (IsLoaded)
                    UpdateGalaxyImage();
            }
        }
        #endregion

        #region Methods
        private void OnOptionsChanged()
        {
            if (OptionsChanged != null)
                OptionsChanged();
        }

        private void UpdateGalaxyImage()
        {
            try
            {
                var imageSource = new BitmapImage(
                    new Uri(
                        "vfs:///Resources/Images/Galaxies/" + this.lstGalaxyShape.SelectedItem + ".png",
                        UriKind.Absolute));
                GalaxyImage.Source = imageSource;
            }
            catch {}
        }
        #endregion
    }
}